﻿(function () {
  'use strict';

  angular
    .module('articles.admin')
    .controller('ArticlesAdminListController', ArticlesAdminListController);

  ArticlesAdminListController.$inject = ['ArticlesService', '$scope'];

  function ArticlesAdminListController(ArticlesService, $scope) {
    var vm = this;

    vm.showFooter = false;
    vm.isLoading = false;
    vm.articles = ArticlesService.queryByHttp(false);
    vm.showMore = function () {
      if(vm.articles.length == 0) {
        return;
      }

      vm.isLoading = true;
      var last = vm.articles[vm.articles.length - 1];
      var lastItemTime = last.created;
      var moreArts = [];

      ArticlesService.queryMoreByHttp(false, lastItemTime).then(
        // Suc fn
        function (articles) {
          Array.prototype.push.apply(vm.articles, articles);
        },
        // Error fn
        function () {
          // Do nothing
        }
      )
        ['finally'](function (){
        vm.isLoading = false;
      })
    };

    // Event
    $scope.$on('$getArticleList', function (isGot) {
      if(isGot) {
        vm.showFooter = true;
      }
    });

  }
}());
